// d:\byte\tiktok-applet-v3 - 智慧天气 - 副本\pages\myjifen\myjifen.js
const model = require('../../public/model/env.js');
const serialNumber = model.generateSerialNumbe();
const url = model.api_dev_url(); // 线上环境
const key = model.key();
// const appid = model.appid();
const userid = tt.getStorageSync('userid');
Page({
  data: {
    U_Id: userid,
    // lassName: 'containerdark',
    title: '积分信息',
    jifentongzhi: '每次分享获得10积分，每天最多可获得30积分。',
    jifentongzhi1: '成功邀请好友得20积分，无上限',
    // jifen_zhan: false,
    gaozhitrue: false,
    gaozhi: '',
    orderList: [

      // 其他订单项...
    ],
  },
  onPullDownRefresh: function () {
    this.data.serialNumbers = model.generateSerialNumbe();
    this.setData({
      serialNumbers: this.data.serialNumbers + '1',
    });
    console.log(this.data.serialNumbers);
    // 更新页面数据
    this.fetchData();
  },
  fetchData: function () {
    var that = this;
    // 模拟请求数据
    setTimeout(() => {
      // 积分券数量返回
      const requestUrl = `${url}/index.php/api/points_log.php`;

      const options = {
        url: requestUrl,
        data: {
          user_id: that.data.U_Id,
          key: key
        },
        method: 'GET',
        header: {
          'content-type': 'application/json'
        },
        dataType: 'json',
      };
      // console.log(options);
      tt.request({
        ...options,
        success: (res) => {
          const data = res.data;
          console.log(data.data);
          if (data.code == 200) {
            that.setData({
              orderList: data.data,
            });

          }
        },
        fail: (err) => {
        }
      })

      // 获取积分信息

      // 停止下拉刷新
      tt.stopPullDownRefresh();
    }, 1500);
  },
  //风向
  async onShareAppMessage() {
    const self = this;

    return {
      title: `周易占卜第${self.data.U_Id}封邀请函`,
      desc: "邀请你一起参与周易占卜，一起寻找命运的奥秘",
      path: `/pages/index/index?fromcode=${self.data.U_Id}`,
      // console.log(this.data.U_Id),
      // templateId: "r30qx0gd326b69b383",
      success() {
        // 积分增加
        // console.log(path);
        console.log(userid);
        const requestUrl = `${url}/index.php/api/points_add.php`;

        const options = {
          url: requestUrl,
          data: {

            user_id: self.data.U_Id,
            ticket_title: 901,
            Invitee_open_id: self.data.serialNumbers + self.data.U_Id,
            key: key
          },
          method: 'GET',
          header: {
            'content-type': 'application/json'
          },
          dataType: 'json',
        };
        console.log(options);
        tt.request({
          ...options,
          success: (res) => {
            const data = res.data;
            console.log(data);
            if (data.code == 200) {
              tt.showToast({ title: '积分增加', icon: 'none', duration: 2000 });

            }
            if (data.code == 400) {
              tt.showToast({ title: data.message, icon: 'fail', duration: 2000 });
            }
          },
          fail: (err) => {
          }
        })
        // 积分增加结束

      },
      fail() {
        console.log("转发发布器调起失败");
      },
    };
  },
  // 分享结束
  toggleExpand(e) {
    const index = e.currentTarget.dataset.index;
    const orderList = [...this.data.orderList];
    orderList[index].isExpanded = !orderList[index].isExpanded;
    this.setData({ orderList });
  },
  upper(e) {
    tt.showToast({
      title: '到最顶部了',
      icon: 'none'
    });
  },
  lower(e) {
    tt.showToast({
      title: '底线，没有更多了',
      icon: 'none'
    });
  },
  onLoad: function () {
    // 获取积分信息

    const requestUrl = `${url}/index.php/api/points_log.php`;

    const options = {
      url: requestUrl,
      data: {
        user_id: this.data.U_Id,
        key: key
      },
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      dataType: 'json',
    };
    // console.log(options);
    tt.request({
      ...options,
      success: (res) => {
        const data = res.data;
        console.log(data.data);
        if (data.code == 200) {
          this.setData({
            orderList: data.data,
          });

        }
      },
      fail: (err) => {
      }
    })

    // 获取积分信息


    // 页面加载,标题设置
    tt.setNavigationBarTitle({
      title: this.data.title,
    });
    tt.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#000000',
    });
    // 页面加载,标题设置结束
  }
})